#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#include "iot_pwm.h"
#include "hi_pwm.h"
#include "hi_time.h"

/*========================================
 * 硬件配置 - 修改为GPIO11/PWM2
 ========================================*/
#define BEEP_PWM_PORT        HI_PWM_PORT_PWM2
#define BEEP_GPIO_PIN        HI_IO_NAME_GPIO_11  // 修改为GPIO11
#define NOTE_DURATION_BASE   125000
#define NOTE_INTERVAL        30000

/*========================================
 * 生日歌音符频率表
 ========================================*/
static const uint16_t g_tuneFreqs[] = {
    0,      // 0 休止符
    38223,  // 1 中音Do
    34052,  // 2 中音Re
    30338,  // 3 中音Mi
    28635,  // 4 中音Fa
    25511,  // 5 中音So
    22728,  // 6 中音La
    20249,  // 7 中音Si
    19111,  // 8 高音Do
    17026,  // 9 高音Re
    15169   // 10 高音Mi
};

/*========================================
 * 生日歌简谱
 ========================================*/
static const uint8_t g_scoreNotes[] = {
    5, 5, 6, 5, 8, 7, 0,
    5, 5, 6, 5, 9, 8, 0,
    5, 5, 10, 8, 7, 6, 5, 5
};

static const uint8_t g_scoreDurations[] = {
    4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4
};

/*========================================
 * 蜂鸣器初始化 - 使用GPIO11
 ========================================*/
static void Beep_InitGPIO11(void)
{
    hi_gpio_init();
    // 设置GPIO11为PWM2输出功能
    hi_io_set_func(BEEP_GPIO_PIN, HI_IO_FUNC_GPIO_11_PWM2_OUT);
    hi_io_set_pull(BEEP_GPIO_PIN, HI_IO_PULL_UP);
    hi_pwm_init(BEEP_PWM_PORT);
    hi_pwm_set_clock(PWM_CLK_XTAL);
    printf("蜂鸣器初始化完成 - GPIO11/PWM2\n");
}

/*========================================
 * 播放音乐函数
 ========================================*/
static void Music_PlaySong(void)
{
    size_t scoreLength = sizeof(g_scoreNotes) / sizeof(g_scoreNotes[0]);
    
    printf("开始播放音乐...\n");
    
    for (size_t i = 0; i < scoreLength; i++) {
        uint8_t note = g_scoreNotes[i];
        uint32_t duration = g_scoreDurations[i] * (NOTE_DURATION_BASE / 4);
        
        if (note > 0) {
            uint16_t freqDivisor = g_tuneFreqs[note];
            printf("播放音符: %d, 频率分频值: %d\n", note, freqDivisor);
            
            hi_pwm_start(BEEP_PWM_PORT, freqDivisor / 2, freqDivisor);
            hi_udelay(duration);
            hi_pwm_stop(BEEP_PWM_PORT);
        } else {
            printf("休止符\n");
            hi_udelay(duration);
        }
        hi_udelay(NOTE_INTERVAL);
    }
    
    printf("音乐播放完成\n");
}

/*========================================
 * 主任务函数
 ========================================*/
static void Music_Task(void *arg)
{
    (void)arg;
    
    // 初始化GPIO11蜂鸣器
    Beep_InitGPIO11();
    
    // 播放音乐
    Music_PlaySong();
}

/*========================================
 * 系统入口函数
 ========================================*/
static void Music_Entry(void)
{
    osThreadAttr_t attr = {
        .name = "MusicTask",
        .stack_size = 4096,
        .priority = osPriorityNormal
    };
    
    if (osThreadNew(Music_Task, NULL, &attr) == NULL) {
        printf("创建音乐任务失败!\n");
    } else {
        printf("音乐任务创建成功 - 使用GPIO11驱动\n");
    }
}

SYS_RUN(Music_Entry);